package net.wanho.service.impl;

import net.wanho.po.Goods;
import net.wanho.mapper.GoodsMapper;
import net.wanho.service.GoodsService;
import org.springframework.stereotype.Service;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author 汤小洋
 * @since 2025-09-25
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class GoodsServiceImpl implements GoodsService {

    private final GoodsMapper goodsMapper;

    /**
     * 商品表分页查询
     * @param pageNum
     * @param pageSize
     * @param goods
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo findPage(int pageNum, int pageSize, Goods goods) {
        if(ObjectUtil.isNull(goods)){
            goods = new Goods();
        }

        // 分页
        PageHelper.startPage(pageNum,pageSize);
        List<Goods> goodsList = goodsMapper.select(goods);
        return new PageInfo(goodsList);
    }

    /**
     * 根据主键查询商品表
     * @param goodsId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Goods findById(Serializable goodsId) {
        return goodsMapper.selectById(goodsId);
    }

    /**
     * 商品表新增
     * @param goods
     */
    @Override
    public void add(Goods goods) {
        goodsMapper.insert(goods);
    }

    /**
     * 修改商品表
     * @param goods
     */
    @Override
    public void modify(Goods goods) {
        goodsMapper.update(goods);
    }

    /**
     * 根据主键删除商品表
     * @param goodsId
     */
    @Override
    public void removeById(Serializable goodsId) {
        if(((String)goodsId).contains(",")){
            goodsMapper.deleteByIds(((String)goodsId).split(","));
        }else {
            goodsMapper.deleteById(goodsId);
        }
    }
}
